<template>
    <div class="content-container">
        <el-card>
            <!-- 查询部分 -->
            <el-form :model="query" :inline="true" class="demo-form-inline">
                <el-form-item label="名称">
                    <el-input v-model="query.name" clearable />
                </el-form-item>
                <el-form-item label="主机">
                    <el-input v-model="query.host" clearable />
                </el-form-item>
                <el-form-item label="标签">
                    <el-input v-model="query.tag" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="formQuery(false)">查询</el-button>
                    <el-button type="warning" @click="formQuery(true)">重置</el-button>
                    <el-button type="primary" @click="openDialog(null)">新增</el-button>

                    <el-popconfirm confirm-button-text="确认" cancel-button-text="取消" title="确认删除选中数据?"
                        @confirm="removeRows" width="200px">
                        <template #reference>
                            <el-button type="danger">删除</el-button>
                        </template>
                    </el-popconfirm>
                </el-form-item>
            </el-form>
            <!-- 数据部分 -->
            <el-table max-height="600px" :data="tableData" stripe style="width: 100%; margin-top: 10px"
                ref="tableDataRef">
                <el-table-column type="selection" />
                <el-table-column prop="name" label="名称" />
                <el-table-column prop="host" label="主机">
                    <template #default="scope">
                        {{ scope.row.host }}/{{ scope.row.port }}
                    </template>
                </el-table-column>
                <el-table-column prop="username" label="账号" />
                <el-table-column prop="password" label="密码" />
                <el-table-column prop="tag" label="标签" />
                <el-table-column prop="createTime" label="创建时间" />
                <el-table-column fixed="right" label="操作">
                    <template #default="scope">
                        <el-button @click="openDialog(scope.row)" link type="primary">
                            编辑
                        </el-button>
                        <el-button @click="viewDialog(scope.row)" link type="primary">
                            查看
                        </el-button>
                        <el-button @click="openTerminal(scope.row)" link type="primary">
                            终端
                        </el-button>
                        <el-button @click="openTerminalSource(scope.row)" link type="primary">

                            资源
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div style="margin-top: 20px">
                <el-pagination v-model:current-page="query.current" v-model:page-size="query.size"
                    :page-sizes="[100, 200, 300, 400]" small background layout="total, sizes, prev, pager, next, jumper"
                    :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
            </div>
        </el-card>
    </div>
    <el-dialog v-model="dialogVisible" :title="(formData.id ? '编辑' : '新增') + '机器信息'" width="60%" :show-close="false"
        :close-on-click-modal="false">
        <el-form :rules="rules" :model="formData" class="demo-form-inline" ref="formDataRef">
            <el-form-item prop="name" label="名称">
                <el-input v-model="formData.name" clearable />
            </el-form-item>
            <el-form-item prop="host" label="主机">
                <el-input v-model="formData.host" clearable />
            </el-form-item>
            <el-form-item prop="port" label="端口">
                <el-input v-model="formData.port" clearable />
            </el-form-item>
            <el-form-item prop="username" label="账号">
                <el-input v-model="formData.username" clearable />
            </el-form-item>
            <el-form-item prop="password" label="密码">
                <el-input v-model="formData.password" clearable />
            </el-form-item>
            <el-form-item prop="tag" label="标签">
                <el-input v-model="formData.tag" clearable />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="formSubmit">确认</el-button>
            </span>
        </template>
    </el-dialog>

    <el-dialog v-model="viewVisible" width="60%" :show-close="false" :close-on-click-modal="false">
        <el-input v-model="viewData.name" disabled>
            <template #prepend>名称</template>
        </el-input>
        <el-input v-model="viewData.host" disabled>
            <template #prepend>ip</template>
        </el-input>
        <el-input v-model="viewData.port" disabled>
            <template #prepend>端口</template>
        </el-input>
        <el-input v-model="viewData.username" disabled>
            <template #prepend>账号</template>
        </el-input>
        <el-input v-model="viewData.password" disabled>
            <template #prepend>密码</template>
        </el-input>
        <el-input v-model="viewData.tag" disabled>
            <template #prepend>标签</template>
        </el-input>
        <el-input v-model="viewData.createTime" disabled>
            <template #prepend>创建时间</template>
        </el-input>
        <el-input v-model="viewData.updateTime" disabled>
            <template #prepend>修改时间</template>
        </el-input>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="viewVisible = false">关闭</el-button>
            </span>
        </template>
    </el-dialog>

    <el-dialog v-model="terminalVisible" width="90%" :show-close="false" :close-on-click-modal="false">
        <terminal :websocket-url="'ws://127.0.0.1:9527/ws/ssh/' + token"></terminal>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="terminalVisible = false">关闭</el-button>
            </span>
        </template>
    </el-dialog>

    <el-dialog v-model="resourcelVisible" width="60%" :show-close="false" :close-on-click-modal="false">
        <terminal-source :token="token"></terminal-source>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="resourcelVisible = false">关闭</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts" name="Machine">
import { onMounted, ref, reactive, toRefs } from 'vue'
import type {
    Machine,
    MachineModDTO,
    MachineQueryDTO,
    MachineVo,
} from '@/api/ops/machine/type'
import {
    machineAdd,
    machineDel,
    machineMod,
    machineQuery,
} from '@/api/ops/machine'
import { ElNotification } from 'element-plus'

const formDataRef = ref()
const tableDataRef = ref()
const initQuery = () => {
    return { current: 1, size: 10 } as MachineQueryDTO
}
const state = reactive<any>({
    query: initQuery(),
    tableData: [] as Machine[],
    total: 0,
    dialogVisible: false,
    viewVisible: false,
    terminalVisible: false,
    viewData: {} as MachineVo,
    formData: {} as MachineModDTO,
    rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        host: [{ required: true, message: 'ip不能为空', trigger: 'blur' }],
        port: [{ required: true, message: '端口不能为空', trigger: 'blur' }],
        username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
    },
    token: '',
    resourcelVisible: false,

})
const {
    query,
    tableData,
    total,
    dialogVisible,
    formData,
    rules,
    viewVisible,
    viewData,
    terminalVisible,
    token,
    resourcelVisible,

} = toRefs(state)
const openDialog = (data: any) => {
    if (formDataRef.value) {
        formDataRef.value.resetFields()
    }

    formData.value = data == null ? {} : data
    dialogVisible.value = true
}
const viewDialog = (data: any) => {
    viewVisible.value = true
    viewData.value = data
}
const openTerminal = (data: any) => {
    terminalVisible.value = true
    token.value = data.id
}
const openTerminalSource = (data: any) => {
    resourcelVisible.value = true
    token.value = data.id

}

const handleSizeChange = (size: number) => {
    query.value.size = size
    buildTable()
}
const handleCurrentChange = (current: number) => {
    query.value.current = current
    buildTable()
}
onMounted(() => {
    buildTable()
})
const buildTable = async () => {
    const result = await machineQuery(query.value)
    tableData.value = result.data.rows
    total.value = result.data.total
}

const formQuery = (reset: boolean) => {
    reset ? (query.value = initQuery()) : query.value
    buildTable()
}
const formSubmit = async () => {
    await formDataRef.value?.validate()
    if (formData.value.id) {
        await machineMod(formData.value)
    } else {
        await machineAdd(formData.value)
    }
    dialogVisible.value = false
    await buildTable()
}
const removeRows = async () => {
    const rows = tableDataRef.value.getSelectionRows()
    if (rows.length == 0) {
        ElNotification({
            title: '删除失败',
            message: '请选择要删除的数据',
            type: 'warning',
        })
        return
    }
    const ids = rows.map((each: any) => each.id)
    await machineDel({ ids: ids })
    await buildTable()
}
</script>

<style scoped lang="scss">
.content-container {
    padding: 12px;
}
</style>